﻿using IM.Easy.Core.AppContext;
using IM.Easy.Core.Extensions;
using IM.Easy.Core.Services;
using IM.Easy.Entity.Log;
using IM.Easy.Infrastructure.Query;
using IM.Easy.LogServices.Dtos;
using IM.Easy.LogServices.IServices;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace IM.Easy.LogServices.Services
{
    /// <summary>
    /// 登录注销日志
    /// </summary>
    public class SessionLogService(ISqlSugarClient db) : AppServices(db), ISessionLogService
    {
        /// <summary>
        /// 添加登录日志
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool AddLoginLog(long uid, string userName)
        {
            var log = new SessionLog
            {
                // 设置日志的操作类型为登录
                ActionType = SessionType.Login,
                ActionTime = DateTime.Now,
                UserId = uid,
                UserName = userName,
                Description = "登录成功",
                Terminal = TerminalType.PC,
                TraceIdentifier = ImApp.Current.TraceIdentifier
            };
            return Db.Insertable(log).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 添加注销日志
        /// </summary>
        /// <param name="log">注销日志信息</param>
        /// <returns>操作结果</returns>
        public bool AddLogoutLog(long uid, string userName)
        {
            // 设置日志的操作类型为注销
            var log = new SessionLog
            {
                // 设置日志的操作类型为登录
                ActionType = SessionType.Logout,
                ActionTime = DateTime.Now,
                UserId = uid,
                UserName = userName,
                Description = "注销成功",
                Terminal = TerminalType.PC,
                TraceIdentifier = ImApp.Current.TraceIdentifier
            };
            return Db.Insertable(log).ExecuteCommand() > 0;
        }

        public PageResponse<SessionLog> GetPageList(SessionLogQueryDto query)
        {
            var list = Db.Queryable<SessionLog>();
            var exp = Expressionable.Create<SessionLog>();
            exp = exp
                  .AndIF(query.BeginActionTime.HasValue, s => s.ActionTime >= query.BeginActionTime)
                  .AndIF(query.EndActionTime.HasValue, s => s.ActionTime <= query.EndActionTime)
                  .AndIF(!string.IsNullOrEmpty(query.UserName), s => s.UserName.Contains(query.UserName))
;
            var page = list
                 .Where(exp.ToExpression())
                 .ToPageList<SessionLog, SessionLog>(query);
            return page;
        }

        public SessionLog GetById(Guid id)
        {
            var sessionLog = Db.Queryable<SessionLog>()
                .Includes(s => s.TraceInfo)
                .InSingle(id);
            return sessionLog;
        }

        public bool Update(SessionLog entity)
        {
            // TODO: 添加更新逻辑
            return Db.Updateable(entity).ExecuteCommandHasChange();
        }

        public bool Delete(params Guid[] Id)
        {   
            // TODO: 添加删除逻辑
            return Db.Deleteable<SessionLog>(Id).ExecuteCommandHasChange();
        }
    }
}
